package org.yn.emp.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yn.auth.annotation.PermissionAnnotation;
import org.yn.base.util.AjaxResult;
import org.yn.base.util.PageInfo;
import org.yn.emp.EmployeeDTO;
import org.yn.emp.domain.Employee;
import org.yn.emp.service.IEmployeeService;
import org.yn.query.EmployeeQuery;


import java.util.List;
import java.util.Objects;

@PermissionAnnotation(name="员工管理",descs = "员工管理的api")
@RestController
@RequestMapping("/employee")
@Api(value = "员工管理的api",description = "员工管理的增删改查CRUD")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @PermissionAnnotation(name="通过id查询员工",descs = "通过id查询员工")
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询员工")
    public AjaxResult selectById(@PathVariable Long id){
        try {
            Employee employee = employeeService.selectById(id);
            return  AjaxResult.success(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }
    @PermissionAnnotation(name="查询所有员工",descs = "查询所有员工")
    @GetMapping()
    @ApiOperation(value = "查询所有员工")
    public AjaxResult selectAll(){
        try {
            List<Employee> employee = employeeService.selectAll();
            return  AjaxResult.success(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }
    @PermissionAnnotation(name="新增或修改员工",descs = "新增或修改员工")
    @PostMapping()
    @ApiOperation(value = "新增或修改员工")
    public AjaxResult updateOrInsert(@RequestBody Employee employee){
        try {
            if(Objects.nonNull(employee)){
                if (Objects.nonNull(employee.getId())){
                    employeeService.update(employee);
                }else {
                    employeeService.insert(employee);
                }
            }else {
                return AjaxResult.error("请输入正确的数据");
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

    @PermissionAnnotation(name="通过id删除员工",descs = "通过id删除员工")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除员工")
    public AjaxResult delete(@PathVariable Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 分页查询接口
     */
    @PermissionAnnotation(name="员工分页查询",descs = "员工分页查询接口")
    @PutMapping()
    public AjaxResult queryPage(@RequestBody EmployeeQuery employeeQuery){
        try {
            PageInfo<Employee> pageInfo = employeeService.queryPage(employeeQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    // 根据角色添加保存权限
    @PermissionAnnotation(name="根据员工添加保存角色",descs = "根据员工添加保存角色")
    @PostMapping("/role")
    @ApiOperation(value = "根据员工添加保存角色")
    public AjaxResult insertByEmployeeId(@RequestBody EmployeeDTO employeeDTO) {
        try {
            employeeService.insertByEmployeeId(employeeDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }
    //根据员工id查询对应的角色
    @PermissionAnnotation(name="根据员工id查询对应的角色",descs = "根据员工id查询对应的角色")
    @GetMapping("/role/{id}")
    @ApiOperation(value = "根据员工id查询对应的角色")
    public AjaxResult selectByEmployeeId(@PathVariable("id") Long id){
        try {
            List<Long> roles = employeeService.selectByEmployeeId(id);
            return AjaxResult.success(roles);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


}
